package com.zmm.book.keep.scheduled;

import com.zmm.book.keep.service.MonthBillService;
import com.zmm.util.time.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Calendar;

/**
 * author 900045
 * description:
 * name MonthBillJob
 * date By 2021-10-28 11:12:39
 */
@Component
public class MonthBillJob {

	private final Logger logger = LoggerFactory.getLogger(MonthBillJob.class);
	
	@Resource
	private MonthBillService monthBillService;

	
	/**
	 * 每个月的最后一天 执行
	 * 0 59 23 L * ?  --> L: 执行会报错 建议使用 高版本的 spring 
	 * 
	 * 目前的解决方案 0 59 23 28-31 * ?
	 * 
	 * 
	 * 测试使用: 59 * * * * ?
	 * author: 900045
	 * date: 2021-10-28 16:40:59
	 * throws
	 * return: void
	 **/
	
	@Scheduled(cron = "0 59 23 28-31 * ?")
	public void init (){
		logger.info("定时任务开始执行!");
		final Calendar calendar = Calendar.getInstance();
		/**
		 * 使用代码逻辑进行判断
		 * calendar.get(Calendar.DATE) -- 当前时间
		 * calendar.getActualMaximum(Calendar.DATE) -- 本月最后一个
		 */
		
		if (calendar.get(Calendar.DATE) == calendar.getActualMaximum(Calendar.DATE)) {
			logger.info("到正确时间开始执行!");
			String date = DateUtil.getFormatTime("yyyy-MM");
			monthBillService.jobSearchMonthBillByDate3(date);
		} else {
			logger.info("还没有到月底哦!");
		}
	}
}
